﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JsonSubmitForm.App_Code;

namespace JsonSubmitForm
{
    public partial class UploadPhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EmployeeData emp = new EmployeeData();
                List<string> idList = emp.GetEmployeeID();

                lstEmpID.Items.Add("Please select one");
                foreach (string item in idList)
                {
                    lstEmpID.Items.Add(item);
                }
            }
        }

        protected void cmdUpload_Click(object sender, EventArgs e)
        {
            if ((updPhoto.HasFile) && (lstEmpID.SelectedIndex != 0))
            {
                string filename = updPhoto.FileName;
                string extension = System.IO.Path.GetExtension(filename);
                string empID = lstEmpID.SelectedItem.Text.Trim();

                if (((String.Equals(extension, ".jpg")) || (String.Equals(extension, ".png")) || (String.Equals(extension, ".gif"))))
                {

                    HttpFileCollection uploads = Request.Files;
                    HttpPostedFile upload = uploads[0];

                    // resize image
                    System.Drawing.Image imgToBeResized = System.Drawing.Image.FromStream(upload.InputStream);
                    int orgHeight = imgToBeResized.Height;
                    int orgWidth = imgToBeResized.Width;
                    int maxHeight = 200;
                    int maxWidth = 400;

                    orgHeight = (orgHeight * maxWidth) / orgWidth;
                    orgWidth = maxWidth;

                    if (orgHeight > maxHeight)
                    {
                        orgWidth = (orgWidth * maxHeight) / orgHeight;
                        orgHeight = maxHeight;
                    }

                    System.Drawing.Bitmap btp = new System.Drawing.Bitmap(imgToBeResized, orgWidth, orgHeight);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream();

                    btp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    stream.Position = 0;
                    byte[] image = new byte[stream.Length + 1];
                    stream.Read(image, 0, image.Length);

                    EmployeeData emp = new EmployeeData();
                    Entity.Status stat = emp.UploadPhoto(empID, image);

                    if (stat.flag)
                    {
                        lblError.Text = "Upload success";
                    }
                    else
                    {
                        lblError.Text = "Upload error. " + stat.message;
                    }

                }
                else
                {
                    lblError.Text = "Error: Only image file can be uploaded";
                }
            }
            else
            {
                lblError.Text = "Error: Make sure you have filled in the required fields";
            }
        }
    }
}